package com.auxgroup.hr.front.service.process.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.auxgroup.hr.front.commons.enums.WfStatusEnum;
import com.auxgroup.hr.front.commons.enums.WfTypeEnum;
import com.auxgroup.hr.front.commons.exception.FrontException;
import com.auxgroup.hr.front.commons.utils.DateUtils;
import com.auxgroup.hr.front.commons.utils.ObjectFieldUtils;
import com.auxgroup.hr.front.commons.vo.UserDTO;
import com.auxgroup.hr.front.dao.domain.JobCurrPrimary;
import com.auxgroup.hr.front.dao.domain.PsCEmpAbsentVw;
import com.auxgroup.hr.front.dao.domain.PsCEmpAbsentVwPrimary;
import com.auxgroup.hr.front.dao.domain.PsCEmplCrdDVw;
import com.auxgroup.hr.front.dao.domain.PsCWfCrdDtl;
import com.auxgroup.hr.front.dao.domain.PsCWfCrdHdr;
import com.auxgroup.hr.front.dao.domain.PsCWfEmpVw;
import com.auxgroup.hr.front.dao.repository.PsCEmpAbsentVwRepository;
import com.auxgroup.hr.front.dao.repository.PsCEmplCrdDVwRepository;
import com.auxgroup.hr.front.dao.repository.PsCWfCrdDtlRepository;
import com.auxgroup.hr.front.dao.repository.PsCWfCrdHdrRepository;
import com.auxgroup.hr.front.dao.repository.PsCWfEmpVwRepository;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.process.WfCrdHdrService;

/** 
 * @author lvliuhao 
 * @version 创建时间：2018年7月18日 下午10:06:42 
 * 类说明 
 */
@Service("wfCrdHdrService")
public class WfCrdHdrServiceImpl implements WfCrdHdrService {
	
	private static final Logger logger = LoggerFactory.getLogger(WfCrdHdrServiceImpl.class);
	
	@Autowired
	private PsCWfCrdHdrRepository psCWfCrdHdrRepository;
	
	@Autowired
	private PsCWfCrdDtlRepository psCWfCrdDtlRepository;
	
	@Autowired
	private PsCEmpAbsentVwRepository psCEmpAbsentVwRepository;
	
	@Autowired
	private PsCEmplCrdDVwRepository psCEmplCrdDVwRepository;
	
	@Autowired
	private PsCWfEmpVwRepository psCWfEmpVwRepository;
	
	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;

	@Override
	public PsCWfCrdHdr getWfCrdHdr(String transNum) {
		return psCWfCrdHdrRepository.findOne(transNum);
	}
	
	
	public List<PsCWfCrdDtl> getWfCrdDtl(String transNum){
		PsCWfCrdDtl query = new PsCWfCrdDtl();
		query.setcTransactionNbr(transNum);
		return psCWfCrdDtlRepository.findAll(Example.of(query));
	}

	@Override
	public List<PsCEmpAbsentVw> getAbsents(String emplid) {
		PsCEmpAbsentVw query = new PsCEmpAbsentVw();
		query.setEmplid(emplid);
		query.setEmplRcd(new BigDecimal(0));
		return psCEmpAbsentVwRepository.findAll(Example.of(query));
	}

	@Override
	public List<PsCEmplCrdDVw> getCrds(String emplid) {
		PsCEmplCrdDVw query = new PsCEmplCrdDVw();
		query.setEmplid(emplid);
		query.setEmplRcd(new BigDecimal(0));
		return psCEmplCrdDVwRepository.findAll(Example.of(query));
	}


	@Override
	@Transactional(rollbackFor=FrontException.class)
	public PsCWfCrdHdr saveWfCrdHdr(String transNum, UserDTO user,
			List<PsCWfCrdDtl> dtls,String crdComment) {
		logger.info("save saveLaveHdrInfo start:",dtls);
		PsCWfCrdHdr crdHdr = psCWfCrdHdrRepository.findOne(transNum);
		if(crdHdr==null){
			crdHdr = new PsCWfCrdHdr();
			JobCurrPrimary primary = new JobCurrPrimary();
			primary.setEmplid(user.getEmplId());
			primary.setEmplRcd(new BigDecimal(0));
			PsCWfEmpVw emp = psCWfEmpVwRepository.findOne(primary);//获取个人信息内容
			crdHdr.setcTransactionNbr(transNum);
			crdHdr.setCreatedttm(new Timestamp(System.currentTimeMillis())); // 创建时间
			crdHdr.setLastupddttm(new Timestamp(System.currentTimeMillis())); // 更新时间
			crdHdr.setcWfStatus(WfStatusEnum.WF_STATUS_S.getStatus()); // 流程状态
			crdHdr.setcWfType(WfTypeEnum.WF_STYPE_CRD.getType());// 流程类别
			crdHdr.setCreatedBy(user.getOprId()); // 创建人用户
			crdHdr.setLastupdoprid(user.getOprId()); // 更新者
			objectMapper.map(emp, crdHdr,"notCopyNull_crdHdr");
			ObjectFieldUtils.handleNullValue(crdHdr);
			crdHdr.setcOtherExplain(crdComment);
			crdHdr =  psCWfCrdHdrRepository.save(crdHdr);
		}
		if(dtls!=null&&!dtls.isEmpty()){
			for (int i = 0; i < dtls.size(); i++) {
				saveCrdDtls(dtls.get(i),transNum,i+1);
			}
		}
		return crdHdr;
	}
	
	/**
	 * 保存补卡明细
	 * @param dtl
	 * @param transNum
	 */
	private void saveCrdDtls(PsCWfCrdDtl dtl,String transNum,Integer seqNbr){
		PsCWfCrdDtl query = new PsCWfCrdDtl();
		query.setcTransactionNbr(transNum);
		query.setDateDay(dtl.getDateDay());
		List<PsCWfCrdDtl> nowDtls = psCWfCrdDtlRepository.findAll(Example.of(query));
		if(nowDtls!=null&&!nowDtls.isEmpty()){
			PsCWfCrdDtl nowdtl = nowDtls.get(0);
			objectMapper.map(dtl, nowdtl,"notCopyNull_WfCrdDtl");
			psCWfCrdDtlRepository.save(nowdtl);
			return;
		}
		PsCEmpAbsentVwPrimary absentPrimary = new PsCEmpAbsentVwPrimary();
		absentPrimary.setEmplid(dtl.getEmplid());
		absentPrimary.setEmplRcd(new BigDecimal(0));
		absentPrimary.setDateDay(dtl.getDateDay());
		PsCEmpAbsentVw absent = psCEmpAbsentVwRepository.findOne(absentPrimary);
		if(absent!=null){
			objectMapper.map(absent, dtl);
		}
		List<PsCEmplCrdDVw> crds =getCrds(dtl.getEmplid(),dtl.getDateDay());
		StringBuffer timeDescr = new StringBuffer();
		if(crds!=null&&!crds.isEmpty()){
			for (PsCEmplCrdDVw crd : crds) {
				timeDescr.append(DateUtils.format(crd.getcCardTime(),DateUtils.FORMAT_YYYYMMDDHHMISS));
				timeDescr.append("\r\n");
			}
			dtl.setcCardTimeDescr(timeDescr.toString());
		}
		dtl.setcTransactionNbr(transNum);
		dtl.setSeqnbr(new BigDecimal(seqNbr));
		ObjectFieldUtils.handleNullValue(dtl);
		psCWfCrdDtlRepository.save(dtl);
	}
	
	
	private List<PsCEmplCrdDVw> getCrds(String emplid,Date dateDay) {
		PsCEmplCrdDVw query = new PsCEmplCrdDVw();
		query.setEmplid(emplid);
		query.setEmplRcd(new BigDecimal(0));
		query.setDateDay(dateDay);
		return psCEmplCrdDVwRepository.findAll(Example.of(query));
	}



	@Override
	public void delCrdHdr(String transNum) {
		PsCWfCrdDtl delDtl = new PsCWfCrdDtl();
		delDtl.setcTransactionNbr(transNum);
		psCWfCrdDtlRepository.delete(delDtl);
		PsCWfCrdHdr delHdr = new PsCWfCrdHdr();
		delHdr.setcTransactionNbr(transNum);
		psCWfCrdHdrRepository.delete(delHdr);
		
	}


}
 